/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.milib.display.ui.scroller {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.display.MCDragController;
	import com.googlecode.milib.display.service.MLButton;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;
	import flash.utils.getQualifiedClassName;

	/**
	 * @author Marek Brun
	 */
	public class MCScrollerWithConstantLengthSlider extends AbstractMCScroler implements IMLBroadcasterListener {

		protected var mc_btnSlider : MovieClip;
		protected var sliderLength:Number;
		protected var lengtH:Number;
		protected var btnSlider:MLButton;
		protected var startDragSliderYPosition:Number;
		protected var dragBtnSlider:MCDragController;
		protected var position:Number;
		protected var visibleToTotal:Number;
		private var mc_bg : MovieClip;
		
		public function MCScrollerWithConstantLengthSlider(mc:MovieClip) {
			super(mc);
			mc_btnSlider=MovieClip(mc.getChildByName('btnSlider'));
			mc_bg=MovieClip(mc.getChildByName('bg'));
			var mc_setterSliderLength:MovieClip=MovieClip(mc.getChildByName('setterSliderLength'));
			sliderLength=mc_setterSliderLength.height;
			mc.removeChild(mc_setterSliderLength);			lengtH=mc_bg.height*mlmc.mc.scaleY;
			mc_bg.height=lengtH;
			mlmc.mc.scaleY=1;
			
			btnSlider=getBtnSlider();
			btnSlider.addListener(this);
			
			dragBtnSlider=new MCDragController(mc_btnSlider, btnSlider, false);
			dragBtnSlider.addListener(this);
			
			if(getQualifiedClassName(this).split('::')[1]=='MCScrollerWithConstantLengthSlider'){
				setPosition(0, 1);
			}
		}
		
		/*abstract*/ protected function getBtnSlider():MLButton {
			return mlmc.getMLButton('btnSlider');
		}
		
		override public function setLength(newLength:Number):void {
			lengtH=newLength;
			mc_bg.height=lengtH;
			draw();
		}
		
		override public function getLength():Number {
			return lengtH;
		}
		
		protected function draw():void {
			mc_btnSlider.visible=visibleToTotal<1;
			mc_btnSlider.y=(lengtH-getSliderLength())*position;
		}
		
		protected function getSliderLength():Number {
			return sliderLength;
		}
		
		/**
		 * Sets the slider in position
		 * 
		 * @param position number from 0 to 1
		 * @param visibleToTotal dependence of visible content (eg. text) to total length (visible/total)
		 */
		override public function setPosition(position:Number, visibleToTotal:Number):void {
			this.position=position;
			this.visibleToTotal=visibleToTotal;
			draw();
			broadcastEvent(event_VisibletoTotalChanged);
		}
		
		/**
		 * @return position number from 0 to 1 (determined by slider positin)
		 */
		override public function getPosition():Number {
			return getPositionBySliderY(mc_btnSlider.y);
		}
		
		override public function getVisibleToTotal():Number {
			return visibleToTotal;
		}
		
		public function getPositionBySliderY(sliderY:Number):Number {
			return Math.max(0, Math.min(1, sliderY/(lengtH-getSliderLength())));
		}
		
//********************************************************************************************
//		events for MCScrollerWithConstantLengthSlider
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case dragBtnSlider:
					switch(ev.event) {
						case dragBtnSlider.event_NewDragXY:
							if(!scrolled){ break; }
							scrolled.setScroll(getPositionBySliderY(dragBtnSlider.getY()));
						break;
					}
				break;
			}
		}
		
	}
}
